package com.sgb.kjwl.viewmodel.self.packers;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.self.ResponseList;
import com.sgb.kjwl.model.entity.self.SelfTeamOfferEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamOfferActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 * @author WYJ
 * @date 2020/8/12
 */
public class SelfTeamOfferModel extends BaseViewModel implements XRecyclerView.LoadingListener {

    private SelfTeamOfferActivity mActivity;

    private int mCurPager = 1;
    private Map<String, Object> map = new HashMap<>();

    public SelfTeamOfferModel(SelfTeamOfferActivity mActivity, SelfRepertory mRepertory) {
        this.mActivity = mActivity;
    }

    public void refreshAndLoadMore(boolean isRefresh) {
        map.clear();
        mCurPager = isRefresh ? 1 : mCurPager + 1;
        map.put("page", mCurPager);
        map.put("limit", AppConstant.PAGER_LIMIT);
        // 首选企业编号，如果企业编号为空，则传用户编号，两者不能同时为空，不能同时都有值
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("companyNo", "");
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.put("companyNo", companyNo);
            map.put("userNo", "");
        }
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTeamPrice(map).enqueue(new Callback<BaseEntity<ResponseList<List<SelfTeamOfferEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ResponseList<List<SelfTeamOfferEntity>>>> call, Response<BaseEntity<ResponseList<List<SelfTeamOfferEntity>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    if (mCurPager <= 1) {
                        mActivity.showEmpty();
                    }
                    return;
                }

                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    mActivity.updateViews(response.body().getData().getList(), mCurPager);
                } else {
                    if (mCurPager <= 1) {
                        mActivity.showEmpty();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ResponseList<List<SelfTeamOfferEntity>>>> call, Throwable t) {
                if (mCurPager <= 1) {
                    mActivity.showEmpty();
                }
            }
        });
    }

    public void deleteSelfWorkOffer(SelfTeamOfferEntity entity) {
        map.clear();
        map.put("no", String.valueOf(entity.getNo()));
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().deleteTeamPrice(map)
                .enqueue(new Callback<BaseEntity<Object>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                        DialogHelper.dismissProgressDialog();
                        BaseEntity<Object> body = response.body();
                        if (body == null || body.getState() == null) {
                            return;
                        }
                        if (body.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            mActivity.deleteUpdateView(entity);
                        } else {
                            MToast.showToast(mActivity, body.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                        MToast.showToast(mActivity, "删除失败！");
                    }
                });
    }

    @Override
    public void onRefresh() {
        refreshAndLoadMore(true);
    }

    @Override
    public void onLoadMore() {
        refreshAndLoadMore(false);
    }

    public void getTeamOffer(String no) {
        APIManager.getInstance().getPickAPI().getUrlSetting("workquotionStatus").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    String url = data.getData().getUrl() + "?no=" +no;
                    WebViewX5HostActivity.start(mActivity, url);
                }
            }
        });
    }
}
